
$(function() {

	$("input[id^='mold_s']:visible").autocomplete(Test.mold);
	
	Tip.emp('申请人');

	$("select[id^='way_s']:visible").multiselect({
		minWidth: 140,
		selectedList: 4
	}).multiselect("uncheckAll");
	
	$("select[id^='type_s']:visible").multiselect({
		minWidth: 140,
		selectedList: 4
	}).multiselect("uncheckAll");
});

var ExpressReqCustomer = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否添加？")) {
					return false;
				}
							
				var form = $("#form_add")[0];

				var body = { };
				
				body.name = form.name.value;
				body.type = form.type.value;
				body.way = form.way.value;
				body.payment = form.payment.value;				
				
				body.address = form.address.value;
				body.recipient = form.recipient.value;
				body.mobile = form.mobile.value;
				
				body.measure = form.measure.value;
				body.quantity = form.quantity.value;
				
				body.weight1 = form.weight1.value;
				body.weight2 = form.weight2.value;
							
				body.content = form.content.value;
				
				body.mold = form.mold.value;
				body.emp = form.emp.value;
				body.date = form.date.value;
				
				$(this).dialog("close");

				$.post('express.php?do=RequirementCustomer.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},	

	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'express.php?do=RequirementCustomer.edit&id=' + id;
		var c2 = 'express.php?do=RequirementCustomer.view&id=' + id;
		var c3 = 'express.php?do=RequirementCustomer.copy&id=' + id;
		
		var cs = 'express.php?do=RequirementCustomer.status&id=' + id;
		
		var c5 = 'express.php?do=RequirementCustomer.reason&id=' + id;
		
		var a1 = ['编辑信息', 'url', 'ExpressReqCustomer.dialog_edit_', '650x400'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];
		
		var a3 = ['复制信息', 'url', 'ExpressReqCustomer.dialog_copy_', '650x400'];
		
		var a5 = ['暂停货运', 'url', 'ExpressReqCustomer.dialog_stop', '650x400'];
		var a6 = ['取消货运', 'url', 'ExpressReqCustomer.dialog_cancel', '650x400'];
		
		// 货运申请
		if (status == 1) {
			var d1 = 'express.php?do=RequirementCustomer.verify&id=' + id;
			var b1 = ['审核信息', 'url', 'ExpressReqCustomer.dialog_verify', '650x400'];
			
			var d2 = 'express.php?do=RequirementCustomer.upload&id=' + id;
			var b2 = ['上传附件', 'url', 'ExpressReqCustomer.dialog_upload', '650x400'];
			
			var s = d1 + ", " + c1 + ", " + c2 + ", " + c3 + ", " + d2;
			
			return popup(e, [b1, a1, a2, a3, b2], s);
		}

		// 等待安排
		if (status == 2) {			
			var s = c1 + ", " + c2 + ", " + c3 + ", " + c5 + ", " + c5;

			return popup(e, [a1, a2, a3, a5, a6], s);
		}

		// 已经安排
		if (status == 3) {			
			var s = c2 + ", " + c3 + ", " + c5 + ", " + c5;

			return popup(e, [a2, a3, a5, a6], s);
		}
		
		// 暂停安排
		if (status == 5) {
			var d1 = 'express.php?do=RequirementCustomer.status_continue&id=' + id + p2;
			var b1 = ['继续安排', 'url', 'ExpressReqCustomer.list_continue'];
			
			var s = d1 + ", " + c2;

			return popup(e, [b1, a2], s);
		}
		
		// 取消安排
		if (status == 6) {
			var d1 = 'express.php?do=RequirementCustomer.status_restart&id=' + id + p2;
			var b1 = ['重启安排', 'url', 'ExpressReqCustomer.list_restart'];
			
			var s = d1 + ", " + c2;

			return popup(e, [b1, a2], s);
		}
			
		var s = c2 + ", " + c3;

		return popup(e, [a2, a3], s);
	},
	
	dialog_verify: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=RequirementCustomer.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},		

					"不提交": function() {
						if (!confirm("确认是否不提交？")) {
							return false;
						}				
						
						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 9;
						
						$(this).dialog("close");
						
						var url = 'express.php?do=RequirementCustomer.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}				

						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 2;
						
						$(this).dialog("close");
						
						var url = 'express.php?do=RequirementCustomer.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	/**
	 * 编辑
	 */
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {			
			"关闭": function() {
				$(this).dialog("close");
			},
	
			"修改": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_edit")[0];
	
				var body = {};
				body.id = form.id.value;
				
				body.name = form.name.value;
				body.type = form.type.value;
				body.way = form.way.value;
				body.payment = form.payment.value;				
				
				body.address = form.address.value;
				body.recipient = form.recipient.value;
				body.mobile = form.mobile.value;
				
				body.measure = form.measure.value;
				body.quantity = form.quantity.value;
				
				body.weight1 = form.weight1.value;
				body.weight2 = form.weight2.value;
							
				body.content = form.content.value;
				
				body.mold = form.mold.value;
				body.emp = form.emp.value;
				body.date = form.date.value;
	
				$(this).dialog("close");
	
				var url = 'express.php?do=RequirementCustomer.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_edit(url, title, xy);
	},

	/**
	 * 复制
	 */
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = {};
				body.name = form.name.value;
				body.type = form.type.value;
				body.way = form.way.value;
				body.payment = form.payment.value;				
				
				body.address = form.address.value;
				body.recipient = form.recipient.value;
				body.mobile = form.mobile.value;
				
				body.measure = form.measure.value;
				body.quantity = form.quantity.value;
				
				body.weight1 = form.weight1.value;
				body.weight2 = form.weight2.value;
							
				body.content = form.content.value;
				
				body.mold = form.mold.value;
				body.emp = form.emp.value;
				body.date = form.date.value;

				$(this).dialog("close");

				var url = 'express.php?do=RequirementCustomer.addPost';

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	dialog_upload: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {			
			"关闭": function() {
				$(this).dialog("close");
			},
	
			"上传": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_edit")[0];
	
				var body = {};
				body.id = form.id.value;			
				body.content = form.content.value;
	
				var filename = $("#filename").val();				
				var s = "express.php?do=RequirementCustomer.ajax_upload&id=" + body.id + "&name=" + filename;
		
				$('#userfile').uploadifySettings('script', s);
				$('#userfile').uploadifyUpload();
			
				// $(this).dialog("close");
	
				var url = 'express.php?do=RequirementCustomer.uploadPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_edit(url, title, xy);
	},

	/**
	 * 暂停
	 */
	dialog_stop: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=RequirementCustomer.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否暂停？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };
						body.id = form.id.value;
						body.reason = form.reason.value;
						body.status = 5;

						$(this).dialog("close");

						var url = 'express.php?do=RequirementCustomer.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	/**
	 * 取消
	 */
	dialog_cancel: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('express.php?do=RequirementCustomer.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否取消？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };
						body.id = form.id.value;
						body.reason = form.reason.value;
						body.status = 6;

						$(this).dialog("close");

						var url = 'express.php?do=RequirementCustomer.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	list_restart: function (url, view) {
		$.getJSON('express.php?do=RequirementCustomer.ajax_emp', function(json) {
			if (json.status == 1) {
				if (confirm("是否要重新安排？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	list_continue: function (url, view) {
		$.getJSON('express.php?do=RequirementCustomer.ajax_emp', function(json) {
			if (json.status == 1) {
				if (confirm("是否要继续安排？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	plan: function (id) {
		var url = "express.php?do=Plan.view&id=" + id;
		Common.dialog_view_(url, '货运安排', '800x500');
	}
};
